/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FormShowStok.java
 *
 * Created on May 5, 2011, 11:52:48 PM
 */

package com.kifas.ui.stok;

import com.kifas.model.xml.PersistStatus;
import com.kifas.model.xml.StokOpnameModel;
import com.kifas.service.MasterService;
import com.kifas.service.StokOpnameService;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author dhamarsu
 */
public class FormPopulateStokManual extends javax.swing.JInternalFrame {
    private MasterService masterService = new MasterService();
    private StokOpnameService stokOpnameService = new StokOpnameService();
    private List<StokOpnameModel>  listStok;

    /** Creates new form FormShowStok */
    public FormPopulateStokManual() {
        initComponents();
        listStok = new ArrayList<StokOpnameModel>();
        refreshTable();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        btnGenerate = new javax.swing.JButton();
        btnRefresh = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblStok = new javax.swing.JTable();

        setClosable(true);
        setIconifiable(true);
        setTitle("Form Generate Stok");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Action"));
        jPanel1.setName("jPanel1"); // NOI18N

        btnGenerate.setText("Generate");
        btnGenerate.setName("btnGenerate"); // NOI18N
        btnGenerate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGenerateActionPerformed(evt);
            }
        });

        btnRefresh.setText("Refresh");
        btnRefresh.setName("btnRefresh"); // NOI18N
        btnRefresh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRefreshActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(btnGenerate, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnRefresh, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(465, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(btnGenerate)
                .addComponent(btnRefresh))
        );

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        tblStok.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "Gudang", "Nama", "Kadaluarsa", "Qty", "Tanggal"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Double.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        tblStok.setName("tblStok"); // NOI18N
        jScrollPane1.setViewportView(tblStok);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 701, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 241, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnGenerateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGenerateActionPerformed
        // TODO add your handling code here:
        try{
            PersistStatus p = stokOpnameService.getPopulateStok();
            listStok = new ArrayList<StokOpnameModel>();
            refreshTable();
            JOptionPane.showMessageDialog(null, p.getReason(), p.getStatus(), JOptionPane.INFORMATION_MESSAGE);
        }catch(Exception e){
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, "Error generate stok ....", "failed", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_btnGenerateActionPerformed

    private void btnRefreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRefreshActionPerformed
        // TODO add your handling code here:
        listStok = stokOpnameService.getAvailable();
        refreshTable();
    }//GEN-LAST:event_btnRefreshActionPerformed

    private void refreshTable(){
        tblStok.setAutoCreateColumnsFromModel(false);
        tblStok.setModel(new StokTableModel(listStok));
    }


    private class StokTableModel extends AbstractTableModel{
        private List<StokOpnameModel>  listStok;

        public StokTableModel(List<StokOpnameModel> listStok) {
            this.listStok = listStok;
        }

        @Override
        public int getRowCount() {
            return listStok.size();
        }

        @Override
        public int getColumnCount() {
            return 5;
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
                StokOpnameModel s = listStok.get(rowIndex);
                switch(columnIndex){
                    case 0: return s.getGudang().getNamaGudang();
                    case 1: return s.getBarang().getNamaBarang();
                    case 2: return s.getBulanKadaluarsa();
                    case 3: return s.getQty();
                    case 4: return new SimpleDateFormat("dd/MM/yyyy").format(s.getTanggal());
                    default: return "";
                }
        }

        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex) {            
            return false;
        }

        @Override
        public Class<?> getColumnClass(int columnIndex) {
            if (columnIndex == 3){
                return BigDecimal.class;
            }
            return super.getColumnClass(columnIndex);
        }




    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnGenerate;
    private javax.swing.JButton btnRefresh;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblStok;
    // End of variables declaration//GEN-END:variables

}
